// Aux functions
//////////////////////////////////////////////////////

var jsonStruct = function (name, url, created, modifier, creator, modified, size, isDocument, fileExtension, nodeRef, thumbId) {
      return {
            "name" : name,
            "contentUrl" : url,
            "creationDate" : '' + created,
            "versionLabel" : '1.0',
            "isLatestVersion" : true,
            "lastModifiedBy" : modifier,
            "createdBy" : creator,
            "lastModificationDate" : ''+modified,
            "contentStreamFileName" : name,
            "contentStreamLength" : size,
            "contentThumbnailStreamFileName" : '',
            "isFile" : isDocument,
            "leaf" : isDocument,
            "fileNameExtension": fileExtension,
            "contentStreamContent" : '',
            "objectId" : nodeRef,
            "thumbId" : thumbId
       };
};

/////////////////////////////////////////////////////



var json = {'docNodes':[]};
var ref = {};

var basePath = '/app:company_home/st:sites';
var folderFilter = ' AND EXACTTYPE:"cm:folder"';
var documentFilter = ' AND EXACTTYPE:"cm:content"';
var maxDepth = 20;
var oneMoreLevel = '/*';
var l=0;

// level 0 - get all sites

ref['l0'] = [];

// get the sites
SiteBasePath = basePath+'/*/cm:documentLibrary';
var querys = 'PATH:"'+SiteBasePath+'" '+folderFilter;
var sites = search.luceneSearch(querys);

for each (var site in sites) 
{
  var _node = jsonStruct  ( site.parent.name, site.parent.url, site.properties['cm:created'], site.properties['cm:modifier'],
            site.properties['cm:creator'], site.properties['cm:modified'], site.size,
            site.isDocument, '',site.nodeRef,'' );

  _node.docNodes = [];
 
  json.docNodes[site.id] = _node;
  ref['l0'][site.id] = json.docNodes[site.id];

}



for (l=1; l<maxDepth; l++)
{
  SiteBasePath += oneMoreLevel;
  
  ref['l'+l] = [];
  
  // recupera le cartelle
  var queryf = 'PATH:"'+SiteBasePath+'" '+folderFilter;
  var nodesf = search.luceneSearch(queryf);
  
  // recupera i documenti
  var queryd = 'PATH:"'+SiteBasePath+'" '+documentFilter;
  var nodesd = search.luceneSearch(queryd);
  
  // caso base: non ci sono ulteriori livelli
  if (nodesd.length == 0 && nodesf.length == 0) break;
  
  
  // folders
  for each(var node in nodesf) 
  {  
    
    if (ref['l'+(l-1)][node.parent.id] != null)
    {
      var _node = jsonStruct  ( node.name, node.url,node.properties['cm:created'], node.properties['cm:modifier'],
                               node.properties['cm:creator'], node.properties['cm:modified'], node.size,
                               node.isDocument, '',node.nodeRef,'' );
      
      _node.docNodes = [];
      
      ref['l'+(l-1)][node.parent.id].docNodes[node.id] = _node;
      
      ref['l'+l][node.id] = ref['l'+(l-1)][node.parent.id].docNodes[node.id];
            
    }
    
  }
  
  
  //docs
  for each(var node in nodesd) 
  {  
    
    if (node.parent.isContainer && ref['l'+(l-1)][node.parent.id] != null)
      {
        var _node = jsonStruct  ( node.name, node.url,node.properties['cm:created'], node.properties['cm:modifier'],
                                 node.properties['cm:creator'], node.properties['cm:modified'], node.size,
                                 node.isDocument, /*node.name.split('\\.').pop()*/'',node.nodeRef,'' )
        ref['l'+(l-1)][node.parent.id].docNodes[node.id] = _node;
       
      }
   
  }    
  
}



model.json = jsonUtils.toJSONString(json);
